﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DALLayer.Base;
using DataDefinition;

namespace DALLayer.Providers
{
    public class VehicleModelTypeYearDataProvider : DataAccessBase
    {
        #region [Constructor]
        /// <summary>
        /// Initializes a new instance of the <see cref="VehicleModelTypeYearDataProvider" /> class.
        /// </summary>
        public VehicleModelTypeYearDataProvider() { }
        #endregion

        #region [Public Methods]
              
        /// <summary>
        /// Get vehicle model year details by id or all vehicle model years
        /// </summary>
        /// <param name="vehicleModelTypeYearID">vehicle model type year ID</param>
        /// <param name="selectById">select by id</param>
        /// <returns>data set</returns>
        public AutoFixDataSet GetModelYearDetails(int vehicleModelTypeYearID, bool selectById)
        {
            AutoFixDataSet dataSet = null;

            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"SELECT VehicleModelTypeYear.* FROM VehicleModelTypeYear");
                //if its a selection by id
                if (selectById == true)
                {
                    //Add the sql string to retrieve data based on VehicleModelTypeYearID
                    base.CommandTextBuilder.Append(@" WHERE (VehicleModelTypeYearID = @VehicleModelTypeYearID)");
                    //Add the params needed to retrieve data
                    base.DBParams.Add(@"@VehicleModelTypeYearID", vehicleModelTypeYearID);
                }

                bool success = false;
                //Check if it is success
                success = base.ExecuteReader();
                //If it is successful
                if (success == true)
                {
                    //Initial the dataset
                    dataSet = new AutoFixDataSet();
                    //Fill the table
                    dataSet.VehicleModelTypeYear.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            finally
            {
                //Close connection
                base.FinalizeConnection();
            }
            //Return the filled dataset
            return dataSet;
        }
        /// <summary>
        /// Get model type year image
        /// </summary>
        /// <param name="vehicleModelTypeID">model id</param>
        /// <returns>dataset</returns>
        public AutoFixDataSet GetModelTypeYearImageDetails(int vehicleModelTypeID)
        {
            AutoFixDataSet dataSet = null;

            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"SELECT VehicleModelTypeYear.VehicleModelTypeImageURL");
                base.CommandTextBuilder.Append(@" FROM ((VehicleModel INNER JOIN
                         VehicleModelType ON VehicleModel.VehicleModelID = VehicleModelType.VehicleModelID) INNER JOIN
                         VehicleModelTypeYear ON VehicleModelType.VehicleModelTypeID = VehicleModelTypeYear.VehicleModelTypeID)");

                //Add the sql string to retrieve data based on VehicleModelID
                base.CommandTextBuilder.Append(@"WHERE (VehicleModelType.VehicleModelTypeID = @VehicleModelTypeID)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@VehicleModelTypeID", vehicleModelTypeID);

                bool success = false;
                //Check if it is success
                success = base.ExecuteReader();
                //If it is successful
                if (success == true)
                {
                    //Initial the dataset
                    dataSet = new AutoFixDataSet();
                    //Fill the table
                    dataSet.ModelTypeImage.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            finally
            {
                //Close connection
                base.FinalizeConnection();
            }
            //Return the filled dataset
            return dataSet;
        }

        /// <summary>
        /// The method inserts vehicle model type year to the table
        /// </summary>
        /// <param name="vehicelModelTypeID">vehicle model type id</param>
        /// <param name="vehicleModelTypeImageURL">vehicle model type image url</param>
        /// <param name="vehicleYear">vehicle year</param>
        /// <param name="isActive">is active</param>
        /// <returns>true or false</returns>
        public bool InsertVehicleModelTypeYear(int vehicelModelTypeID, string vehicleModelTypeImageURL, int vehicleYear, bool isActive)
        {

            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"
                    INSERT INTO VehicleModelTypeYear(VehicleModelTypeID, VehicleModelTypeImageURL, VehicleYear, IsActive)
                    VALUES(@VehicleModelTypeID, @VehicleModelTypeImageURL, @VehicleYear, @IsActive)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@VehicleModelTypeID", vehicelModelTypeID);
                base.DBParams.Add(@"@VehicleModelTypeImageURL", vehicleModelTypeImageURL);
                base.DBParams.Add(@"@VehicleYear", vehicleYear);
                base.DBParams.Add(@"@IsActive", isActive);
                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception)
            {
                success = false;
                //Log here
            }
            //return success
            return success;
        }
        /// <summary>
        /// The method inserts vehicle model type year to the table and returns the row
        /// </summary>
        /// <param name="vehicelModelTypeID">vehicle model type id</param>
        /// <param name="vehicleModelTypeImageURL">vehicle model type image url</param>
        /// <param name="vehicleYear">vehicle year</param>
        /// <param name="isActive">is active</param>
        /// <returns>int</returns>
        public int InsertVehicleModelTypeYearScalar(int vehicelModelTypeID, string vehicleModelTypeImageURL, int vehicleYear, bool isActive)
        {

            int result = 0;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"
                    INSERT INTO VehicleModelTypeYear(VehicleModelTypeID, VehicleModelTypeImageURL, VehicleYear, IsActive)
                    VALUES(@VehicleModelTypeID, @VehicleModelTypeImageURL, @VehicleYear, @IsActive)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@VehicleModelTypeID", vehicelModelTypeID);
                base.DBParams.Add(@"@VehicleModelTypeImageURL", vehicleModelTypeImageURL);
                base.DBParams.Add(@"@VehicleYear", vehicleYear);
                base.DBParams.Add(@"@IsActive", isActive);
                //get the result                
                result = base.ExecuteScalar();
               

            }
            catch (Exception)
            {
                //Log here
            }
            //return success
            return result;
        }
        /// <summary>
        /// The method updates vehicle model type year to the table
        /// </summary>
        /// <param name="vehicleModelTypeYearID">vehicle Model Type Year ID</param>
        /// <param name="vehicleModelTypeID">vehicle Model Type ID</param>
        /// <param name="vehicleModelTypeImageUrl">vehicle Model Type Image Url</param>
        /// <param name="vehicleYear">vehicle Year</param>
        /// <param name="isActive">is active</param>
        /// <returns>true or false</returns>
        public bool UpdateVehicleModelTypeYear(int vehicleModelTypeYearID, int vehicleModelTypeID, string vehicleModelTypeImageUrl, int vehicleYear, bool isActive)
        {
            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"
                    UPDATE VehicleModelTypeYear
                    SET VehicleModelTypeID = @VehicleModelTypeID,
                        VehicleModelTypeImageURL = @VehicleModelTypeImageURL,
                        VehicleYear = @VehicleYear,
                        IsActive = @IsActive                       
                        WHERE VehicleModelTypeYearID = @VehicleModelTypeYearID");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@VehicleModelTypeYearID", vehicleModelTypeYearID);
                base.DBParams.Add(@"@VehicleModelTypeID", vehicleModelTypeID);
                base.DBParams.Add(@"@VehicleModelTypeImageURL", vehicleModelTypeImageUrl);
                base.DBParams.Add(@"@VehicleYear", vehicleYear);
                base.DBParams.Add(@"@IsActive", isActive);

                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception)
            {
                success = false;
                //Log here
            }
            //return success
            return success;
        }
        #endregion
    }
}
